#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <cstdio>

const int N = 105;

int n, k;
std::map<std::vector<int>, long long> f[N][N];
int a[N];
int cur, las;

int main() {
	std::freopen("diyiti.in", "r", stdin);
	std::freopen("diyiti.out", "w", stdout);

	std::cin >> n >> k;
	for(int i = 1; i <= n; ++i) std::cin >> a[i];
	std::sort(a + 1, a + n + 1);
	/*
	std::vector<int> b(a + 1, a + n + 1);
	b.erase(std::unique(b.begin(), b.end()), b.end());
	for(int i = 1; i <= n; ++i) a[i] = std::lower_bound(b.begin(), b.end(), a[i]) - b.begin() + 1;
	*/
	f[0][0][std::vector<int>(k, 0)] = 0;
	for(int i = 1; i <= n; ++i) {
		auto upd = [&](const std::vector<int>& v, long long x, int j) {
			if(j && v[j] == v[j-1]) return;
			if(v[j] == n/k) return;
			long long cost = x;
			if(v[j] == 0) cost -= a[i];
			if(v[j] == n/k-1) cost += a[i];
			auto v0(v);
			int p = j;
			int t = v0[p];
			++v0[p];
			while(p+1 < v0.size() && v0[p] > v0[p+1]) std::swap(v0[p], v0[p+1]), ++p;
			if(!f[i][t].count(v0)) {
				f[i][t][v0] = cost;
			} else {
				f[i][t][v0] = std::min(f[i][t][v0], cost);
			}
		};
		if(a[i] != a[i-1]) {
			for(const auto& e : f[i-1][0]) {
				for(int j = 0; j < k; ++j) {
					upd(e.first, e.second, j);
				}
			}
		} else {
			for(int t = 0; t <= n / k; ++t) {
				for(const auto& e : f[i-1][t]) {
					for(int j = 0; j < k; ++j) {
						if(e.first[j] <= t) upd(e.first, e.second, j);
					}
				}
			}
		}
		for(int t = n / k; t >= 0; --t) {
			for(const auto& e : f[i][t+1]) {
				const auto& v = e.first;
				long long x = e.second;
				if(!f[i][t].count(v)) f[i][t][v] = x;
				else f[i][t][v] = std::min(f[i][t][v], x);
			}
		}
		/*
		std::cerr << "i = " << i << "\n";
		for(int t = 0; t < k; ++t) {
			std::cerr << "t = " << t << "\n";
			for(const auto& [v, c] : f[i][t]) {
				for(int x : v) std::cerr << x << " "; std::cerr << " = " << c << "\n";
			}
		}
		*/
	}
	std::vector<int> v0(k, n/k);
	if(!f[n][1].count(v0)) std::cout << -1 << "\n";
	else std::cout << f[n][1][v0] << "\n";

	return 0;
}
